# -*- coding: utf-8 -*-
import unittest
from collections import defaultdict

from model.gamestate import *
from game_results import *
from presenter_mock import PresenterMock
from results_general import *

WORDCOUNT = 5
PLAYERCOUNT = 4
ROUNDCOUNT = 100020


class GameResultsTest(unittest.TestCase):
    def create_round(self, p1, p2):
        game_round = GameRound(0, [], None, (p1, p2))
        for i in range(len(p1.words)):
            if i % 3 == 0:
                result = WordGuessResult.GUESS
                self.player_guessed_count[p1.name] += 1
                self.player_described_count[p2.name] += 1
                pair = (p1.name, p2.name)
                self.pairs_count[pair] += 1
            elif i % 3 == 1:
                result = WordGuessResult.FAIL
            elif i % 3 == 2:
                result = WordGuessResult.PUT_BACK

            word_guess_result = WordGuessResult(p1.words[i],
                                                result, 5)
            game_round.words.append(word_guess_result)
        return game_round

    def setUp(self):
        self.player_guessed_count = defaultdict(lambda: 0)
        self.player_described_count = defaultdict(lambda: 0)
        self.pairs_count = defaultdict(lambda: 0)
        self.curr_round_count = 0
        self.Phase = Phase.GAME_RESULTS
        self.playersList = []
        for name in range(PLAYERCOUNT):
            words = []
            for i in range(WORDCOUNT):
                words.append(str(i))
            ownerWords = []
            for word in words:
                ownerWords.append(Word('Player' + str(name), word))
            player = Player('Player' + str(name), ownerWords)
            self.playersList.append(player)
        self.players = Players(self.playersList)
        self.hat = Hat([])
        self.rounds = []

        for first in range(0, PLAYERCOUNT, 2):
            self.curr_round_count += 1
            round = self.create_round(self.playersList[first],
                                      self.playersList[first + 1])
            self.rounds.append(round)
            round = self.create_round(self.playersList[first + 1],
                                      self.playersList[first])
            self.rounds.append(round)

        self.settings = Settings(WORDCOUNT, 20, 3)
        self.gamestate = GameState(self.players,
                                   self.hat, None,
                                   self.Phase, self.rounds,
                                   None, self.settings)
        self.gamestate.players.\
            set_next_pair_strategy(TraditionalStrategy(self.playersList))

    def test_creating_result_table(self):
        result_from_pair, result_for_person_guessed,\
            result_for_person_described,\
            result_for_person_guessed_and_described =\
            creating_result_table(self.gamestate)
        for name in self.player_guessed_count:
            self.assertEquals(self.player_guessed_count[name],
                              result_for_person_guessed[name])
        for name in self.player_described_count:
            self.assertEquals(self.player_described_count[name],
                              result_for_person_described[name])
        for name in self.player_described_count:
            self.assertEquals(self.player_described_count[name],
                              result_for_person_described[name])

    @unittest.expectedFailure
    def test_new_game(self):
        self.presenter = PresenterMock(Phase.WELCOME)
        widget = GameResults(self.gamestate, self.presenter)
        widget.new_game()
        self.assertEquals(self.presenter.is_phase_changed, True)
        self.assertEquals(GameState(None, None, None,
                                    None, None, None, None),
                          widget.gamestate)


if __name__ == '__main__':
    unittest.main()
